<template>
  <div class="relative w-full h-full overflow-y-auto">
    <PageHeader :title="t('order_lists_detail.订单详情')">
      <template #right>
        <el-button type="primary" @click="goBack">
          <SvgIcon name="iconfanhui" color="#fff" class="mr4"></SvgIcon
          >{{ t('order_lists_detail.返回上级页面') }}</el-button
        >
      </template>
    </PageHeader>
    <Step @refresh="getDetail" :detail="orderDetail" v-show="orderDetail.id"/>

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('order_lists_detail.订单信息') }}:</div>
    <el-table
      :show-header="false"
      :data="tableData"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
      :span-method="arraySpanMethod"
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>
    <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('order_lists_detail.收货人信息') }}:</div>
    <el-table
      :show-header="false"
      :data="tableData2"
      style="width: 100%"
      :cell-class-name="cellClassName"
      border
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>
    <div class="mt15 mb15 pl5 color-333333 font-700" v-if="hasSend">{{ t('order_lists_detail.物流信息') }}:</div>
    <el-table
        v-if="hasSend"
        :show-header="false"
        :data="tableData4"
        style="width: 100%"
        :cell-class-name="cellClassName"
        border
    >
      <el-table-column prop="label1" />
      <el-table-column prop="value1" />
      <el-table-column prop="label2" />
      <el-table-column prop="value2" />
    </el-table>
    <Kaipiao :orderDetail="orderDetail" />

    <div class="mt15 mb15 pl5 color-333333 font-700">{{ t('order_lists_detail.商品信息') }}:</div>
    <el-table
      :data="orderDetail.saleSkus"
      style="width: 100%"
      border
      class="cover-table"
    >
      <el-table-column :label="t('order_lists_detail.序号')" width="100" type="index" />
      <el-table-column prop="info" :label="t('order_lists_detail.商品信息')">
        <template #default="{ row }">
          <div class="flex_aic">
            <img class="product-img flex-shrink-0" :src="row.skuSpecDetailImage" alt="" />
            <div class="ml10 w-380 text-left">
              <div>{{ row["skuName" + locale] }}</div>
              <div>
                {{ row["skuClassify1Name" + locale] }} >
                {{ row["skuClassify2Name" + locale] }}
              </div>
              <div class="flex_aic">
                {{ t('order_lists_detail.规格') }}：
                <span class="goods_spec flex_aic gap-4 flex-wrap">
                  <span v-for="spec in row.skuSpecDetail" :key="spec.specId"
                    >{{ spec["specName" + locale] }}-{{
                      spec["specDetailName" + locale]
                    }}</span
                  >
                </span>
              </div>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="skuStockPrice" :label="`${t('order_lists_detail.单价')}(${t('order_lists_detail.元')})`" width="160">
        <template #default="{ row }">
          <span>￥{{ row.skuStockPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="skuCount" :label="t('order_lists_detail.数量')" width="160" />
      <el-table-column :label="`${t('order_lists_detail.抵扣金额')}（${t('order_lists_detail.活动')}/${t('order_lists_detail.优惠券')}/${t('order_lists_detail.积分')}）`" width="320">
        <template #default="{ row }">
          <div>
            {{ row.payDiscountActivity }}/{{ row.payDiscountCoupon }}/{{
              row.payDiscountPoint
            }}
          </div>
          <div>{{ t('order_lists_detail.共计') }}：{{ row.payDiscount }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="payRealSku"
        :label="`${t('order_lists_detail.实付金额')}(${t('order_lists_detail.不含运费')})`"
        width="160"
      />
      <el-table-column prop="payRealPostage" :label="t('order_lists_detail.运费')" width="160" />
    </el-table>
    <div class="order_detail_total">
      <!-- <span class="color-333333"
        ><span>{{ t('order_lists_detail.商品总金额') }}(¥{{ orderDetail.payRealSku }}) + {{ t('order_lists_detail.运费') }}(¥{{ orderDetail.payRealPostage }})</span></span
      > -->
      <span class="amount_total">{{ t('order_lists_detail.订单实付金额') }}(¥{{ orderDetail.payReal }})</span>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, watch, onMounted, watchEffect } from "vue";
import { useRouter, useRoute } from "vue-router";
import { useI18n } from "vue-i18n";
import Step from "./components/step.vue";
import Api from "@/config/api/api";
import Kaipiao from "./components/kaipiao.vue";

const { t, locale } = useI18n();
const router = useRouter();
const route = useRoute();
const orderDetail = ref({});
const tableData = ref([]);
const tableData2 = ref([]);

const tableData3 = ref([
  {
    label1: t('order_lists_detail.是否需要开票'),
    value1: t('order_lists_detail.否'),
  },
]);
const hasSend = ref(false)
const tableData4 = ref([]);

watchEffect(() => {
  tableData.value = [
    // {
    //   label1: t('order_lists_detail.订单类型'),
    //   value1: t('order_lists_detail.普通订单'),
    //   label2: t('order_lists_detail.订单号'),
    //   value2: orderDetail.saleNo,
    // },
    {
      label1: t('order_lists_detail.店铺名称'),
      value1: orderDetail.value["tenantName" + locale.value],
      label2: t('order_lists_detail.订单号'),
      value2: orderDetail.value.saleNo,
      // label2: t('order_lists_detail.支付方式'),
      // value2: t('order_lists_detail.在线支付'),
    },
    // {
    //   label1: t('order_lists_detail.订单备注'),
    //   value1: "--",
    // },
  ];
  tableData2.value = [
    {
      label1: t('order_lists_detail.会员名称'),
      value1: orderDetail.value.vipNickName,
      label2: t('order_lists_detail.收货人'),
      value2: orderDetail.value.vipAddressName,
    },
    {
      label1: t('order_lists_detail.收货人手机号'),
      value1: orderDetail.value.vipAddressPhone,
      label2: t('order_lists_detail.收货地址'),
      value2:
        orderDetail.value["vipAddressProvince" + locale.value] +
        orderDetail.value["vipAddressCity" + locale.value] +
        orderDetail.value["vipAddressArea" + locale.value] +
        orderDetail.value.vipAddressAddress + orderDetail.value.vipAddressDetail
    },
  ];
  tableData4.value = [
    {
      label1: t('order_lists_detail.物流公司'),
      value1: orderDetail.value.sendName,
      label2: t('order_lists_detail.物流单号'),
      value2: orderDetail.value.sendNo,
    }
  ];
});

watch(
  () => locale.value,
  (newVal) => {
    tableData.value[1].value1 = orderDetail.value["tenantName" + newVal];
  }
);

const step = ref(1);
const goBack = () => {
  router.back();
};

const cellClassName = ({ row, column, rowIndex, columnIndex }) => {
  if (columnIndex % 2 === 0) {
    return "warning-column winth-20";
  } else {
    return "winth-30";
  }
};

const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (
    (columnIndex === 2 && rowIndex === 2) ||
    (columnIndex === 3 && rowIndex === 2)
  ) {
    return [0, 0];
  } else if (columnIndex === 1 && rowIndex === 2) {
    return [1, 3];
  } else {
    return [1, 1];
  }
};

const getDetail = () => {
  Api.getOrderDetail(route.query.id).then((res) => {
    orderDetail.value = res.data;

    tableData.value[0].value1 = res.data["tenantName" + locale.value]
    tableData.value[0].value2 = res.data["saleNo"]
    tableData2.value[0].value1 = res.data["vipNickName"]
    tableData2.value[0].value2 = res.data["vipAddressName"]
    tableData2.value[1].value1 = res.data["vipAddressPhone"]
    tableData2.value[1].value2 = res.data["vipAddressProvince" + locale.value] +
        res.data["vipAddressCity" + locale.value] +
        res.data["vipAddressArea" + locale.value] +
        res.data.vipAddressAddress + res.data.vipAddressDetail
    hasSend.value = res.data["sendTime"] !== null;
    tableData4.value[0].value1 = res.data["sendName"];
    tableData4.value[0].value2 = res.data["sendNo"];
  });
};

onMounted(()=> {
  getDetail();
})
</script>

<style lang="scss" scoped>
.mt50 {
  margin-top: 50px;
}

:deep(.warning-column) {
  background-color: #f8f9fa;
}
:deep(.winth-20) {
  width: 20%;
}
:deep(.winth-30) {
  width: 30%;
}
.w-380 {
  width: 380px;
}
.cover-table {
  .product-img {
    width: 100px;
    height: 100px;
    object-fit: cover;
  }
}

.order_detail_total {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100%;
  margin-top: 10px;
  margin-bottom: 20px;
  padding-right: 5px;
  font-size: 14px;
  font-weight: 700;
  height: 45px;
  border-radius: 6px;
  background: #fff;
  box-shadow: 0 0 8px #9999990d, 0 0 8px #9999990d;
  .amount_total {
    color: #ff2b4e;
  }
}
</style>
